﻿using QLBH.Models;
using QLBH.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLBH.Modules
{
    public partial class frmAdd_Edit_Units : Form
    {
        private eType eType = eType.ADD;
        private UnitsModel model;
        public UnitsModel Model
        {
            get { return model; }
            set { model = value; }
        }

        public frmAdd_Edit_Units(eType eType)
        {
            InitializeComponent();
            this.eType = eType;
            model = new UnitsModel();
        }

        private void frmAdd_Edit_Units_Load(object sender, EventArgs e)
        {
            if (this.eType == eType.ADD)
            {
                this.Text = "Thêm Đơn Vị Tính";
            }
            else
            {
                this.Text = "Sửa Đơn Vị Tính";
                //load data griview to textbox
                txtName.Text = Model.Name;
                rtbDescription.Text = Model.Comments;
            }
        }
        private bool CheckValidData()
        {
            try
            {
                DataInputHelper.CheckTextData(txtName.Text, "Tên đơn vị tính", 30);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Đơn Vị Tính", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtName.SelectAll();
                txtName.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(rtbDescription.Text, "Ghi Chú", 200);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Đơn Vị Tính", MessageBoxButtons.OK, MessageBoxIcon.Error);
                rtbDescription.SelectAll();
                rtbDescription.Focus();
                return false;
            }
            return true;
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (CheckValidData())
            {
                Model.Name = txtName.Text;
                Model.Comments = rtbDescription.Text;
                //check dieu kien truoc khi chay
                if (eType == eType.ADD)
                {
                    try
                    {

                        UnitsInsertResponse result = WSAccess.Insert<UnitsModel, UnitsInsertResponse>(model, MainForm.Instance.Shop_id + "/units/insert");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    try
                    {
                        WSAccess.Update<UnitsModel>(model, Model.Id + "/units/edit");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                this.DialogResult = DialogResult.None;
            }
        }
    }
    public class UnitsInsertResponse : ResponseBase
    {
        private int units_id;

        public int Units_id
        {
            get { return units_id; }
            set { units_id = value; }
        }
    }
}
